<template>
  <view class="beforeLogin">
    <view class="before-content">
      <image :src="baseImgUrl + 'beforeBg.png'" class="login-bg"></image>
      <image :src="baseImgUrl + 'beforeLogo.png'" class="login-icon"></image>

      <view> 欢迎使用场所信息企业端小程序！ </view>

      <view class="btn-wrap">
        <button type="default" @click="handleWxLogin" class="submit-btn">
          微信登录
        </button>
        <!-- <button type="default" v-else class="submit-btn" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">
          授权手机号
        </button> -->
      </view>
    </view>
  </view>
</template>

<script>
  import {
    isLoading
  } from "../../utils/util";
  import {
    userDecrypt,
    wxLogin
  } from "@/api/api.js";
  let WXBizDataCrypt = require("@/utils/WXBizDataCrypt");

  export default {
    data() {
      return {
        baseImgUrl: this.$baseImgUrl,
        code: "",
        flag: true,
      };
    },
    onShow() {
      let _this = this;
    },
    methods: {
      bindUserInfo() {
        uni.getUserProfile({
          desc: "获取你的昵称、头像",
          success: (res) => {
            getApp().globalData.userInfo = res.userInfo;
            this.flag = false;
          },
          fail: (res) => {
            console.log(res, "错误");
            //用户按了拒绝按钮
            wx.showModal({
              title: "警告",
              content: "您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!",
              showCancel: false,
              confirmText: "返回授权",
              success: function(res) {
                console.log(res)
                if (res.confirm) {
                  console.log("用户点击了“返回授权”");
                }
              },
            });
          },
        });
      },
      onGetPhoneNumber(e) {
        let phoneInfo = e.detail;
        let encryptedData = phoneInfo.encryptedData;
        let iv = phoneInfo.iv;

        if (e.detail.errMsg == "getPhoneNumber:ok") {
          userDecrypt({
            code: this.code,
          }).then((res) => {

            let data = {
              openid: res.result.openid,
              phone,
              avateor: getApp().globalData.userInfo.avatarUrl,
              encryptedData,
              sessionKey: res.result.session_key,
              iv,
              userName: getApp().globalData.userInfo.nickName,
            };

            wxLogin(data).then((res2) => {
              console.log(res2, "登录返回");
              if (res2.code === 200) {
                const {
                  refreshToken,
                  token,
                  userInfo
                } = res2.result;
                uni.setStorageSync("refreshToken", refreshToken); //刷新token 参数值
                uni.setStorageSync("X-MINI-TOKEN", token);
                uni.setStorageSync("user", userInfo);
                uni.reLaunch({
                  url: "/pages/home/home",
                });
              }
            });
          });
        }
      },

      handleWxLogin() {
        isLoading(true)
        uni.login({
          success: (res) => {
            this.code = res.code;
            userDecrypt({
                code: this.code,
              }).then((res) => {
                let data = {
                  openid: res.result.openid,
                  // avateor: getApp().globalData.userInfo.avatarUrl,
                  // encryptedData,
                  sessionKey: res.result.session_key,
                  // iv,
                  // userName: getApp().globalData.userInfo.nickName,
                };

                wxLogin(data).then((res2) => {
                    if (res2.code === 200) {
                      const {
                        refreshToken,
                        token,
                        userInfo
                      } = res2.result;
                      uni.setStorageSync("refreshToken", refreshToken); //刷新token 参数值
                      uni.setStorageSync("X-MINI-TOKEN", token);
                      uni.setStorageSync("user", userInfo);
                      uni.reLaunch({
                        url: "/pages/home/home",
                      });
                    }
                  })
                  .finally(() => {
                    isLoading(false)
                  })
              })
              .finally(() => {
                isLoading(false)
              })
          },
        });
      }
    },
  };
</script>

<style lang="scss">
  .beforeLogin {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    background: #2491fc;

    .before-content {
      position: relative;
      width: 712rpx;
      height: 589rpx;
      border-radius: 16rpx;
      text-align: center;
      background-color: #fff;

      padding: 70rpx 35rpx 100rpx 35rpx;

      color: #273923;
      font-size: 36rpx;
      font-weight: bold;

      .login-bg {
        position: absolute;
        top: -61%;
        right: 10%;
        width: 488rpx;
        height: 370rpx;
      }

      .login-icon {
        width: 167rpx;
        height: 167rpx;
        border-radius: 50%;
      }

      .btn-wrap {
        margin-top: 80rpx;
      }
    }
  }
</style>